
//
// 110629 - AH - Created. (Extracted from SaveChatXmlGeneratorFactory).
// 120224 - AH - (Kryddar *) Changes related to that the ScanPaintResult is
//               replaced by ConversationElement.
//

package org.soekrat.engine.io.responsexml;

import static org.soekrat.engine.io.IOUtil.encode;

import java.io.CharArrayWriter;
import java.io.PrintWriter;
import java.util.List;

import org.soekrat.AttributeObject;
import org.soekrat.engine.SoekratEngine;
import org.soekrat.engine.io.xmlutil.XmlContentGenerator;
import org.soekrat.engine.motor.ConversationElement;
import org.soekrat.engine.pit.Pit;
import org.soekrat.engine.visitor.Line;
import org.soekrat.engine.visitor.Lines;

/**
 * <p>
 *   {@link XmlContentGenerator} for generating the XML as part of enveloping
 *   XML to represent a Visitor/Soekrat conversation exchange.
 * </p>
 * @author Arne Halvorsen (AH)
 * @author Laetitia Le Chatton (LLC)
 */
public final class ExchangeXmlContentGenerator implements XmlContentGenerator
{
  private final Pit m_pit;
  
  /**
   * <p>
   *   Constructor.
   * </p>
   * @param engine {@link SoekratEngine}.
   */
  public ExchangeXmlContentGenerator(SoekratEngine engine)
  {
    if (engine==null)
    {
      throw new NullPointerException("engine");
    }
    
    m_pit=engine.getPit(); 
  }
  
  // Specified in XmlContentGenerator.
  public String compile(Object c) 
  {
    if (c instanceof ConversationElement)
    {
      ConversationElement ce=(ConversationElement)c;
      
      Lines lines=(Lines)ce.getAttribute(ConversationElement.LINES);
      
      if (lines!=null && lines.size()>0)
      {
        CharArrayWriter caw=new CharArrayWriter();
        PrintWriter pw=new PrintWriter(caw);
        
        pw.println();
        pw.println("<exchange>");
        
        pw.println();
        pw.println("<visitor>");
        for (Line line : lines) 
        {
          String txt=line.getText();
          txt=(txt==null) ? "" : txt.trim();
          if (txt.length()>0)
          {
            pw.println();
            pw.print("  <line>");
            pw.print(encode(txt));
            pw.println("</line>");
          }
        }
        
        pw.println();
        pw.println("</visitor>");
        
        @SuppressWarnings("unchecked")
        List<AttributeObject> fsw=
          (List<AttributeObject>)ce.getAttribute(ConversationElement.SW);
        @SuppressWarnings("unchecked")
        List<AttributeObject> fw=
          (List<AttributeObject>)ce.getAttribute(ConversationElement.W);
        
        if (!fsw.isEmpty() || !fw.isEmpty())
        {
          boolean wheel=!fw.isEmpty();
          pw.println("<soekrat wheel='"+wheel+"'>");
          
          if (!fsw.isEmpty())
          {
            SaveChatXmlGeneratorFactory.writeSpareWheelXml(fsw, pw);
          }
          else
          {
            SaveChatXmlGeneratorFactory.writeWheelXml(fw, pw, m_pit);
          }
          
          pw.println();
          pw.println("</soekrat>");
        }
        
        pw.println();
        pw.println("</exchange>");
        
        pw.flush();
        return caw.toString();
      }
    }
    return "";
  }
  
}
